<template>
  <div class="popular-market">
    <h2>{{ title }}</h2>
    <el-table :data="paginatedTableData" style="width: 100%" height="200">
      <!-- 动态列 -->
      <el-table-column
        v-for="column in columns"
        :key="column.prop"
        :prop="column.prop"
        :label="column.label"
      />
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      class="pagination"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="tableData.length"
      layout="prev, pager, next"
      @current-change="handlePageChange"
    />
  </div>
</template>

<script setup>
import { ref, computed } from "vue";

// 定义 props
const props = defineProps({
  title: {
    type: String,
    default: "热门市场（成交地区）"
  },
  tableData: {
    type: Array,
    required: true
  },
  columns: {
    type: Array,
    required: true
  }
});

// 分页相关状态
const currentPage = ref(1); // 当前页码
const pageSize = ref(5); // 每页显示条数

// 计算属性：根据当前页码和每页条数，返回分页后的数据
const paginatedTableData = computed(() => {
  const start = (currentPage.value - 1) * pageSize.value;
  const end = start + pageSize.value;
  return props.tableData.slice(start, end);
});

// 处理页码变化
const handlePageChange = page => {
  currentPage.value = page;
};
</script>

<style scoped>
.popular-market {
  padding: 20px;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  background-color: #fff;
}

h2 {
  margin-bottom: 20px;
  font-size: 18px;
  font-weight: bold;
}

.pagination {
  margin-top: 20px;
  text-align: right;
}
</style>
